package net.lab1024.sa.admin.module.business.datasource.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import net.lab1024.sa.admin.module.business.datasource.domain.form.QueryHistoryQueryForm;
import net.lab1024.sa.admin.module.business.datasource.domain.form.SqlExecuteForm;
import net.lab1024.sa.admin.module.business.datasource.domain.vo.QueryHistoryVO;
import net.lab1024.sa.admin.module.business.datasource.domain.vo.QueryResultVO;
import net.lab1024.sa.admin.module.business.datasource.service.DatabaseManagerService;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import net.lab1024.sa.admin.module.business.datasource.domain.vo.DatabaseObjectVO;

/**
 * 数据库管理
 *
 * @Author 1024创新实验室-主任:卓大
 * @Date 2024-01-01 00:00:00
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
@RestController
@Tag(name = "数据库管理")
@RequestMapping("/database-manager")
public class DatabaseManagerController {

    @Resource
    private DatabaseManagerService databaseManagerService;

    @GetMapping("/objects/{datasourceId}")
    @Operation(summary = "获取数据库对象列表 @author 卓大")
    public ResponseDTO<List<DatabaseObjectVO>> getDatabaseObjects(@PathVariable Long datasourceId,
                                                       @RequestParam(required = false) String objectType) {
        return databaseManagerService.getDatabaseObjects(datasourceId, objectType);
    }

    @PostMapping("/execute-query")
    @Operation(summary = "执行SQL查询 @author 卓大")
    public ResponseDTO<QueryResultVO> executeQuery(@Valid @RequestBody SqlExecuteForm form) {
        return databaseManagerService.executeQuery(form);
    }

    @PostMapping("/execute-update")
    @Operation(summary = "执行SQL更新 @author 卓大")
    public ResponseDTO<QueryResultVO> executeUpdate(@Valid @RequestBody SqlExecuteForm form) {
        return databaseManagerService.executeUpdate(form);
    }

    @GetMapping("/table-structure/{datasourceId}")
    @Operation(summary = "获取表结构信息 @author 卓大")
    public ResponseDTO<QueryResultVO> getTableStructure(@PathVariable Long datasourceId,
                                                       @RequestParam String tableName) {
        return databaseManagerService.getTableStructure(datasourceId, tableName);
    }

    @GetMapping("/query-history/{datasourceId}")
    @Operation(summary = "获取查询历史 @author 卓大")
    public ResponseDTO<Page<QueryHistoryVO>> getQueryHistory(@PathVariable Long datasourceId,
                                                            @Valid QueryHistoryQueryForm form) {
        form.setDatasourceId(datasourceId);
        return databaseManagerService.getQueryHistory(form);
    }
} 